//
//  MTCConcreteConfirmationTYDZController.m
//  XTCloud
//
//  Created by QA on 2017/9/21.
//  Copyright © 2017年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MTCConcreteConfirmationTYDZController.h"
#import "AutoTextLableView.h"
#import "NSDictionary+DataFormat.h"
#import "NSKeyedArchiverUserLogin.h"

#import "MTCSubmitSuccessViewController.h"
#import "MTCSubmitErrorViewController.h"

#import "WebServiceCommonRequest.h"

@interface MTCConcreteConfirmationTYDZController ()

@property (nonatomic, strong) NSDictionary *contractData;

@end

@implementation MTCConcreteConfirmationTYDZController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle: @"合同变更明细"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getPassingParameter:(id)object
{
    if(!object) return;
    if ([object isKindOfClass:[NSDictionary class]]){
        NSDictionary *contractdic = object;
        self.contractData = contractdic;
        [self newContractCeateHandle:contractdic];
        
        NSString *HTBH = [contractdic objectFormatForKey:@"合同编号"];
        if(!HTBH){
            [self setTitle: @"合同信息确认"];
            UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self
                                                                          action:@selector(submitContractSelector:)];
            self.navigationItem.rightBarButtonItem = leftButton;
        } else {
            [self setTitle: @"合同修改确认"];
            UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"修改" style:UIBarButtonItemStyleDone target:self
                                                                          action:@selector(submitContractSelector:)];
            self.navigationItem.rightBarButtonItem = leftButton;
        }
    }
    
}

#pragma mark 新建合同处理

- (void)newContractCeateHandle:(NSDictionary*)contractDic
{
    //客户名称
    NSDictionary *KHMCDic = [contractDic objectFormatForKey:@"客户名称"];
    NSString *KHMC = [KHMCDic objectFormatStringForKey:@"客户名称"];
    
    //合同性质
    NSDictionary *HTXZDic = [contractDic objectFormatForKey:@"合同性质"];
    NSString *HTXZ = [HTXZDic objectFormatStringForKey:@"Definition"];
    
    NSString *HTMC = [contractDic objectFormatStringForKey:@"合同名称"];
    NSString *QDFL = [contractDic objectFormatStringForKey:@"签订方量"];
    NSString *QDRQ = [contractDic objectFormatStringForKey:@"签订日期"];
    BOOL FLKZ = [[contractDic objectFormatFloatNumberForKey:@"方量控制"] boolValue];
    NSString *JSDW = [contractDic objectFormatStringForKey:@"建设单位"];
    NSString *SGDW = [contractDic objectFormatStringForKey:@"施工单位"];
    NSString *DSFDW = [contractDic objectFormatStringForKey:@"第三方单位"];
    NSString *GDDZ = [contractDic objectFormatStringForKey:@"工地地址"];
    NSString *YSJL = [contractDic objectFormatStringForKey:@"运输距离"];
    NSString *GDLXR = [contractDic objectFormatStringForKey:@"工地联系人"];
    NSString *GDLXDH = [contractDic objectFormatStringForKey:@"工地联系电话"];
    NSString *YWRY = [contractDic objectFormatStringForKey:@"业务人员"];
    NSString *YWYDH = [contractDic objectFormatStringForKey:@"业务员电话"];
    NSString *BZ = [contractDic objectFormatStringForKey:@"备注"];
    
    NSMutableArray *contract = [NSMutableArray array];
    [self addContractKeyName:@"客户名称" defaults:nil format:nil value:KHMC toArray:contract];
    [self addContractKeyName:@"合同名称" defaults:nil format:nil value:HTMC toArray:contract];
    [self addContractKeyName:@"合同性质" defaults:nil format:nil value:HTXZ toArray:contract];
    [self addContractKeyName:@"签订方量" defaults:@"0立方" format:@"%@方" value:QDFL toArray:contract];
    [self addContractKeyName:@"是否方量控制" defaults:(FLKZ ? @"是":@"否") format:nil value:nil toArray:contract];
    [self addContractKeyName:@"签订日期" defaults:nil format:nil value:QDRQ toArray:contract];
    [self addContractKeyName:@"建设单位" defaults:@"未填写" format:nil value:JSDW toArray:contract];
    [self addContractKeyName:@"施工单位" defaults:@"未填写" format:nil value:SGDW toArray:contract];
    [self addContractKeyName:@"第三方单位" defaults:@"未填写" format:nil value:DSFDW toArray:contract];
    
    //天元站点定制
    NSDictionary *BHLXDic = [self.contractData objectFormatForKey:@"编号类型"];
    NSDictionary *GSZDDic = [self.contractData objectFormatForKey:@"归属站点"];
    if(BHLXDic && GSZDDic){
        NSString *BHLX = [BHLXDic objectFormatStringForKey:@"U_name"];
        NSString *GSZD = [GSZDDic objectFormatStringForKey:@"U_name"];
        [self addContractKeyName:@"编号类型" defaults:@"未填写" format:nil value:BHLX toArray:contract];
        [self addContractKeyName:@"归属站点" defaults:@"未填写" format:nil value:GSZD toArray:contract];
    }
    
    [self addContractKeyName:nil defaults:nil format:nil value:nil toArray:contract];
    [self addContractKeyName:@"工地地址" defaults:@"未填写" format:nil value:GDDZ toArray:contract];
    [self addContractKeyName:@"运输距离" defaults:@"0公里" format:@"%@公里" value:YSJL toArray:contract];
    [self addContractKeyName:@"工地联系人" defaults:@"未填写" format:nil value:GDLXR toArray:contract];
    [self addContractKeyName:@"工地联系电话" defaults:@"未填写" format:nil value:GDLXDH toArray:contract];
    [self addContractKeyName:nil defaults:nil format:nil value:nil toArray:contract];
    
    [self addContractKeyName:@"业务人员" defaults:@"未填写" format:nil value:YWRY toArray:contract];
    [self addContractKeyName:@"业务员电话" defaults:@"未填写" format:nil value:YWYDH toArray:contract];
    [self addContractKeyName:@"备      注" defaults:@"未填写" format:nil value:BZ toArray:contract];
    
    [self drawInfoToView:contract];
}

#pragma mark 页面数据绘制

- (void)drawInfoToView:(NSArray*)contract
{
    AutoTextLableView *newView = [[AutoTextLableView alloc] initWithFrame:CGRectMake(10, 70, 300, 0)];
    [newView setDataArray:contract];
    [self.scrollView addSubview:newView];
    
    //滑动层
    CGFloat textLableViewHeight = newView.frame.size.height + newView.frame.origin.y;
    if(textLableViewHeight > self.scrollView.contentSize.height){
        self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, textLableViewHeight + 15);
    }
    
    //白色背景层
    if(textLableViewHeight - 30 > self.backgroundView.frame.size.height){
        CGRect backgroundFrame = self.backgroundView.frame;
        backgroundFrame.size.height = textLableViewHeight - 30;
        self.backgroundView.frame = backgroundFrame;
    }
}

- (void)addContractKeyName:(NSString*)tokey defaults:(id)def format:(id)formatText
                     value:(id)value toArray:(NSMutableArray*)array
{
    if (!tokey) tokey = @"";
    if (!def) def = @"";
    
    if (!value){
        value = def;
        
    } else if([value isKindOfClass:[NSString class]]){
        if([value isEqualToString:@""]) value = def;
        else if (formatText) value = [NSString stringWithFormat:formatText,value];
        
    } else if (formatText) {
        value = [NSString stringWithFormat:formatText,value];
    }
    
    NSDictionary *valueDic = [NSDictionary dictionaryWithObject:value forKey:tokey];
    [array addObject:valueDic];
}

#pragma mark 合同提交

- (void)submitContractSelector:(id)selector
{
    //客户名称
    NSDictionary *KHMCDic = [self.contractData objectFormatForKey:@"客户名称"];
    NSString *KHMC = [KHMCDic objectFormatStringForKey:@"客户ID"];
    
    //合同性质
    NSDictionary *HTXZDic = [self.contractData objectFormatForKey:@"合同性质"];
    NSString *HTXZ = [HTXZDic objectFormatStringForKey:@"Status"];
    
    //制表人员
    UserLoginNSCoding *userInfo = [NSKeyedArchiverUserLogin getUserLoginData];
    NSString *ZBRY = userInfo.username;
    
    //方量控制 1否，2是
    BOOL FLKZ = [[self.contractData objectFormatFloatNumberForKey:@"方量控制"] boolValue];
    
    NSString *HTBH = [self.contractData objectFormatStringForKey:@"合同编号"];
    NSString *HTMC = [self.contractData objectFormatStringForKey:@"合同名称"];
    NSString *QDFL = [self.contractData objectFormatStringForKey:@"签订方量"];
    NSString *QDRQ = [self.contractData objectFormatStringForKey:@"签订日期"];
    NSString *JSDW = [self.contractData objectFormatStringForKey:@"建设单位"];
    NSString *SGDW = [self.contractData objectFormatStringForKey:@"施工单位"];
    NSString *DSFDW = [self.contractData objectFormatStringForKey:@"第三方单位"];
    NSString *GDDZ = [self.contractData objectFormatStringForKey:@"工地地址"];
    NSString *YSJL = [self.contractData objectFormatStringForKey:@"运输距离" defaults:@"0"];
    NSString *GDLXR = [self.contractData objectFormatStringForKey:@"工地联系人"];
    NSString *GDLXDH = [self.contractData objectFormatStringForKey:@"工地联系电话"];
    NSString *YWRY = [self.contractData objectFormatStringForKey:@"业务人员"];
    NSString *YWYDH = [self.contractData objectFormatStringForKey:@"业务员电话"];
    NSString *BZ = [self.contractData objectFormatStringForKey:@"备注"];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setShowLoadingPrompt:self.navigationController labelText:@"提交合同中"];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"HTGL_XSHT_AU_20170112001"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"合同编号" value:HTBH];
    [content addParameterKey:@"客户ID" value:KHMC];
    [content addParameterKey:@"合同名称" value:HTMC];
    [content addParameterKey:@"合同性质" value:HTXZ];
    [content addParameterKey:@"签订方量" value:QDFL];
    [content addParameterKey:@"是否控制方量" value:(FLKZ?@"2":@"1")];
    [content addParameterKey:@"签订日期" value:QDRQ];
    [content addParameterKey:@"建设单位" value:JSDW];
    [content addParameterKey:@"施工单位" value:SGDW];
    [content addParameterKey:@"第三方单位" value:DSFDW];
    [content addParameterKey:@"工地地址" value:GDDZ];
    [content addParameterKey:@"运输距离" value:YSJL];
    [content addParameterKey:@"工地联系人" value:GDLXR];
    [content addParameterKey:@"工地联系电话" value:GDLXDH];
    [content addParameterKey:@"业务人员" value:YWRY];
    [content addParameterKey:@"业务员电话" value:YWYDH];
    [content addParameterKey:@"制表人员" value:ZBRY];
    [content addParameterKey:@"备注" value:BZ];
    
    NSDictionary *BHLXDic = [self.contractData objectFormatForKey:@"编号类型"];
    NSDictionary *GSZDDic = [self.contractData objectFormatForKey:@"归属站点"];
    if(BHLXDic && GSZDDic){
        NSString *BHLX = [BHLXDic objectFormatStringForKey:@"U_code"];
        NSString *GSZD = [GSZDDic objectFormatStringForKey:@"Id"];
        [content addParameterKey:@"编号类型" value:BHLX];
        [content addParameterKey:@"归属站点" value:GSZD];
    }
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        //添加数据，刷新页面
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0){
            firstArray = [result.resultArray firstObject];
        }
        
        //数据处理及显示
        if(firstArray && firstArray.count>0){
            [self showSuccessHUDText:@"提交成功"];
            MTCSubmitSuccessViewController *view = [[MTCSubmitSuccessViewController alloc] init];
            [view setValue:[firstArray firstObject]];
            [self.navigationController pushViewController:view animated:YES];
        } else{
            [self showErrorHUDText:@"提交失败"];
            MTCSubmitErrorViewController *view = [[MTCSubmitErrorViewController alloc] init];
            view.key = @"MTCSubmitErrorViewController";
            view.backResultDelagate = self;
            [self.navigationController pushViewController:view animated:YES];
        }
        
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showErrorHUDText:@"提交失败"];
        MTCSubmitErrorViewController *view = [[MTCSubmitErrorViewController alloc] init];
        view.key = @"MTCSubmitErrorViewController";
        view.backResultDelagate = self;
        [self.navigationController pushViewController:view animated:YES];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showErrorHUDText:@"网络异常"];
        MTCSubmitErrorViewController *view = [[MTCSubmitErrorViewController alloc] init];
        view.key = @"MTCSubmitErrorViewController";
        view.backResultDelagate = self;
        [self.navigationController pushViewController:view animated:YES];
    }];
    
    
}


#pragma mark 重新提交
#pragma mark BackResultDataDelagate

- (void) backResultDataKey:(id)key dataValue:(id)backValue
{
    if ([key isEqualToString:@"MTCSubmitErrorViewController"]) {
        //重新提交合同
        [self submitContractSelector:nil];
    }
}

#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

@end

